package com.rm.vip.mq.demo.receiver;

import org.springframework.jms.listener.SessionAwareMessageListener;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

/**
 * @ProjectName: mq-demo
 * @Auther: GERRY
 * @Date: 2019/2/25 21:35
 * @Description: 消息处理类
 */
public class MyMessageListener implements SessionAwareMessageListener {
    //测试方法
    public void onMessage(Message message, Session session)  throws JMSException {
        System.out.println("--------------------开始接受消息------------------------");
        TextMessage textMsg = (TextMessage) message;
        try {
            if ("重发机制".equals(textMsg.getText())) {
                System.out.println("----------------");
                throw new RuntimeException("故意抛出的异常");
            }
            System.out.println(textMsg.getText());
            System.out.println("--------------------接受消息结束------------------------");
        } catch (Exception e) {
            //此不可省略 重发信息使用，当该代码注释掉之后，该队列消息不能移出队列
            session.recover();
            System.out.println("异常");
        }
    }
}
